<?php
/**
 * @Project NUKEVIET 3.0
 * @Author VINADES.,JSC (contact@vinades.vn)
 * @Copyright (C) 2010 VINADES., JSC. All rights reserved
 * @Createdate 3/9/2010 23:25
 */

if ( ! defined( 'NV_IS_MOD_DOWNLOAD' ) ) die( 'Stop!!!' );
/**
 * nv_setcats()
 *
 * @param mixed $list2
 * @param mixed $id
 * @param mixed $list
 * @param integer $m
 * @param integer $num
 * @return
 */
if(!function_exists("setSubCats")){
    function setSubCats( $list2, $id, $list, $m = 0, $num = 0 )
    {
        $num++;
        $defis = "";
        for ( $i = 0; $i < $num; $i++ )
        {
            $defis .= "--";
        }

        if ( isset( $list[$id] ) )
        {
            foreach ( $list[$id] as $value )
            {
                if ( $value['id'] != $m )
                {
                    $list2[$value['id']] = $value;
                    $list2[$value['id']]['title'] = "|" . $defis . "&gt; " . $list2[$value['id']]['name'];
                    if ( isset( $list[$value['id']] ) )
                    {
                        $list2 = setSubCats( $list2, $value['id'], $list, $m, $num );
                    }
                }
            }
        }
        return $list2;
    }
}
/**
 * nv_listcats()
 *
 * @param mixed $parentid
 * @param integer $m
 * @return
 */
if(!function_exists("getAllCats")){
    function getAllCats( $parentid, $m = 0 )
    {
        global $db;

        $sql = "SELECT * FROM `" . NV_PREFIXLANG . "_download_categories` WHERE `status`=1 ORDER BY `parentid`,`weight` ASC";
        $result = $db->sql_query( $sql );
        $list = array();
        while ( $row = $db->sql_fetchrow( $result ) )
        {
            $list[$row['parentid']][] = array( //
                'id' => ( int )$row['id'], //
                'parentid' => ( int )$row['parentid'], //
                'title' => $row['title'], //
                'alias' => $row['alias'], //
                'description' => $row['description'], //
                'who_view' => ( int )$row['who_view'], //
                'groups_view' => ! empty( $row['groups_view'] ) ? explode( ",", $row['groups_view'] ) : array(), //
                'who_download' => ( int )$row['who_download'], //
                'groups_download' => ! empty( $row['groups_download'] ) ? explode( ",", $row['groups_download'] ) : array(), //
                'weight' => ( int )$row['weight'], //
                'status' => $row['weight'], //
                'name' => $row['title'], //
                'selected' => $parentid == $row['id'] ? " selected=\"selected\"" : "" //
                );
        }

        if ( empty( $list ) )
        {
            return $list;
        }

        $list2 = array();
        foreach ( $list[0] as $value )
        {
            if ( $value['id'] != $m )
            {
                $list2[$value['id']] = $value;
                if ( isset( $list[$value['id']] ) )
                {
                    $list2 = setSubCats( $list2, $value['id'], $list, $m );
                }
            }
        }

        return $list2;
    }
}

global $module_name, $lang_module, $module_data, $nv_Request;

$xtpl = new XTemplate( "block_search.tpl", NV_ROOTDIR . "/themes/" . $module_info['template'] . "/modules/" . $module_file );
$xtpl->assign( 'LANG', $lang_module );
$key = filter_text_input( 'q', 'post', '', 1, NV_MAX_SEARCH_LENGTH );
$cat = $nv_Request->get_int( 'cat', 'post' );
$xtpl->assign( 'keyvalue', $key );
$xtpl->assign( 'FORMACTION', NV_BASE_SITEURL . 'index.php?' . NV_LANG_VARIABLE . '=' . NV_LANG_DATA . '&' . NV_NAME_VARIABLE . '=' . $module_name . '&' . NV_OP_VARIABLE . '=newsearch' );
$query = "SELECT id, title, alias, parentid FROM `" . NV_PREFIXLANG . "_" . $module_data . "_categories` WHERE parentid=0 ORDER BY weight";
$result = $db->sql_query( $query );
$nv_list_cats = getAllCats(0);
foreach ( $nv_list_cats as $cat_i )
{
    if($cat == $cat_i['id'] ){
        $cat_i['selected'] = "selected=\"selected\"";
    }
    $xtpl->assign( 'loop', $cat_i );
    $xtpl->parse( 'main.loop' );
}

//while ( $row = $db->sql_fetchrow( $result ) )
//{
//    $row['select'] = ( $row['id'] == $cat ) ? 'selected=selected' : '';
//    $xtpl->assign( 'loop', $row );
//    /*	$subdata = getsubcat ( $row ['id'], $cat, '--' );
//	if (! empty ( $subdata )) {
//		$xtpl->assign ( 'subcat', $subdata );
//		unset ( $subdata );
//	}*/
//    $xtpl->parse( 'main.loop' );
//}

$xtpl->parse( 'main' );
$content = $xtpl->text( 'main' );



?>